<template>
  <el-row class="login_container">
    <el-col :lg="16" :md="12" class="left">
      <div>
        <div>创新项目申报系统</div>
        <div>
          君子之学必日新，日新者日进也。不日新者必日退，未有不进而不退者。
        </div>
      </div>
    </el-col>
    <el-col :lg="8" :md="12" class="right">
      <h1 class="title">欢 迎 回 来</h1>
      <div>
        <span class="line"></span>
        <span>账号密码登录</span>
        <span class="line"></span>
      </div>
      <el-form ref="formRef" :rules="rules" :model="form" class="w-[250px]">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名">
            <template #prefix>
              <el-icon class="el-input__icon"><UserFilled /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            show-password
            v-model="form.password"
            placeholder="请输入密码"
          >
            <template #prefix>
              <el-icon class="el-input__icon"><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type=""
            round
            class="w-[250px] bg-indigo-500 text-light-200"
            @click="loginBtn"
            :loading="loading"
            >登 录</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button
            round
            type="info"
            class="w-[250px] text-light-200"
            @click="registerBtn"
            >注 册</el-button
          >
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style scoped>
.login_container {
  @apply bg-indigo-500 min-h-screen;
}
.login_container .left,
.login_container .right {
  @apply flex items-center justify-center flex-col;
}
.login_container .right {
  @apply bg-light-400  flex-col;
}
.left > div > div:first-child {
  @apply font-bold text-4xl text-light-100 mb-5;
}
.left > div > div:last-child {
  @apply text-sm text-gray-300;
}

.right .title {
  @apply font-bold text-3xl text-gray-600;
}
.right > div {
  @apply flex items-center justify-center my-5 text-gray-400 space-x-2;
}
.right .line {
  @apply h-[1px] w-16 bg-gray-200;
}
</style>

<script setup>
import { reactive, ref, onMounted, onBeforeUnmount } from "vue";
import { login } from "@/api/apiv1";
import { useRouter } from "vue-router";
import {
  setToken
} from "@/composables/auth";
import { message } from "@/composables/util";
import { useStore } from "vuex";


const router = useRouter();
const store = useStore();

// do not use same name with ref
const form = reactive({
  username: "",
  password: "",
});

const rules = {
  username: [
    { required: true, message: "账号不能为空", trigger: "blur" },
    { min: 4, max: 10, message: "注意长度", trigger: "blur" },
  ],
  password: [{ required: true, message: "密码不能为空", trigger: "blur" }],
};

const formRef = ref(null);

const loading = ref(false);

// 图标引入 已全局引入 此处不用再引
// import { UserFilled, Lock } from "@element-plus/icons-vue";

const loginBtn = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      loading.value = true;

      store.dispatch("login",form).then(res=>{
        store.dispatch("getUser");
        router.push("/");
      }).finally(()=>{
        loading.value = false
      })
    }
  });
};

function onkeyUp(e){
  if(e.key == "Enter"){
    loginBtn()
  }
}

// 添加键盘监听
onMounted(() => {
  document.addEventListener("keyup",onkeyUp)
})

// 移除键盘监听
onBeforeUnmount(()=>{
  document.removeEventListener("keyup",onkeyUp)
})

const registerBtn = () => {
  console.log("register!");
};
</script>
